// 主界面
;
define(function(require, exports, module) {
	'use strict';
	var util = require("util");
	var base = require(CONFIG.BasePath + "js/enterprise/base");
	var router = require("../hash/pageRouter");
	var top = require("../top/top_main");
	var footer = require("../footer/footer_main");
//	var css_main = require("./main.css");
	var css_product = require("./product.css");
	var constant = require("constant");
	var css_common = require();
	var userID;
	var PONO;
	var auditState;
	var pageIndex = 1,
		pageSize = 8,
		totalPageCount = 1,
		totalRecordCount = 0; //分页初始化
	var init = function() {
		userID = base.getUrlParam("userid");
		PONO = base.getUrlParam("bookingid");
		
		top.init(1);
		footer.init();
		router.init();
		$("#productPage").addClass("nav_active");
		$("#head").removeClass("none");
		$("#content").removeClass("none");
		$("#footer").removeClass("none");
		if(base.getData("sid") != null) {
			inquireCurrentEnterpriseDetail();
		}
		initEvent();
	};

	var initEvent = function() {
		inquireFinanceProductPageList();
		inquireFinanceKillProductPageList()
	};
	
	//获得用户信息
	var inquireCurrentEnterpriseDetail = function() {
		base.fnAjax({
			action: "inquireCurrentEnterpriseDetail.json",
			oData: {}, //传递参数
			fnSuccess: function(data) {
				if (data.status == 200) {
					auditState = data.datas[0].auditState;
				} else {}
			}
		});
	};

	//查询推荐产品列表
	var inquireFinanceProductPageList = function() {
		base.fnAjax({
			action: "inquireFinanceProductPageList.json",
			nocode: true,
			oData: {
				pageIndex: pageIndex,
				pageSize: pageSize,
				state: 4,
				productScope: 1
			}, //传递参数
			fnSuccess: function(data) {
				if (data.status == 200) {
					juicer.register("getValueByKey", constant.getValueByKey);
					var tpl = $("#mainFinanceProductListTpl").html();
					var html = juicer(tpl, data);
					$("#recommendProduct").html(html);
					totalPageCount = data.totalPageCount;
					totalRecordCount = data.totalRecordCount;
					//根据右边的内容多来设置左边图片的大小
					var height = $("#recommendProduct").height();
					$(".one").css("height", height);
               
					base.pageControl(
						$("#pagenation"),
						totalRecordCount,
						data.pageIndex,
						data.pageSize,
						function(p) {
							pageIndex = p;
							inquireFinanceProductPageList();
						});

					$(".instantlyFinance").unbind("click").click(function() {
						var productType = $(this).attr("productType");
						if (userID != null && userID != "") {
							if (productType == "6") {
								location.href = '../apply/apply_main1.html?productType=' + productType + "&userID=" + userID + "&PONO=" + PONO;//代销
							} else if (productType == "5") {
								location.href = '../apply/apply_main2.html?productType=' + productType + "&userID=" + userID + "&PONO=" + PONO;//代采
							} else {
								location.href = '../apply/apply_main.html?productType=' + productType + "&userID=" + userID + "&PONO=" + PONO;
							}
						} else if(auditState == 2 || auditState == 4){
							if (productType == "6") {
								location.href = '../apply/apply_main1.html?productType=' + productType; //代销
							} else if (productType == "5") {
								location.href = '../apply/apply_main2.html?productType=' + productType; //代采
							} else {
								location.href = '../apply/apply_main.html?productType=' + productType;
							}
						}else{
							base.alert(2, "您的账户还未进行资质认证，请先进行资质认证!");
							location.href = '../approve/approve_main.html';
						}
					});
					
					$(".productDetail").unbind("click").click(function() {
						var productId = $(this).attr("id");
						location.href = '../product/productDetail.html?productId=' + productId;
					});

				} else {
					base.alert(2, "信息获取失败");
				}
			}
		});
	}
	
	//查询秒杀产品列表
	var inquireFinanceKillProductPageList = function() {
		base.fnAjax({
			action: "inquireFinanceProductPageList.json",
			nocode: true,
			oData: {
				pageIndex: pageIndex,
				pageSize: pageSize,
				productType:2,
				state:4,
				productScope: 1
			}, //传递参数
			fnSuccess: function(data) {
				if (data.status == 200) {
					if(data.datas.length==0){
						$("#kill_product").addClass("none");
					}
					juicer.register("getValueByKey", constant.getValueByKey);
					var tpl = $("#mainFinanceKillProductListTpl").html();
					var html = juicer(tpl, data);
					$("#killProduct").html(html);
					totalPageCount = data.totalPageCount;
					totalRecordCount = data.totalRecordCount;
					var height = $("#killProduct").height();
					$(".two").css("height", height);

					base.pageControl(
						$("#pagenation"),
						totalRecordCount,
						data.pageIndex,
						data.pageSize,
						function(p) {
							pageIndex = p;
							inquireFinanceProductPageList();
						});

					$(".instantlyFinance").unbind("click").click(function() {
						var productType = $(this).attr("productType");
						if (userID != null && userID != "") {
							if (productType == "6") {
								location.href = '../apply/apply_main1.html?productType=' + productType + "&userID=" + userID + "&PONO=" + PONO;//代销
							} else if (productType == "5") {
								location.href = '../apply/apply_main2.html?productType=' + productType + "&userID=" + userID + "&PONO=" + PONO;//代采
							} else {
								location.href = '../apply/apply_main.html?productType=' + productType + "&userID=" + userID + "&PONO=" + PONO;
							}
						} else if(auditState == 2 || auditState == 4){
							if (productType == "6") {
								location.href = '../apply/apply_main1.html?productType=' + productType; //代销
							} else if (productType == "5") {
								location.href = '../apply/apply_main2.html?productType=' + productType; //代采
							} else {
								location.href = '../apply/apply_main.html?productType=' + productType;
							}
						}else{
							base.alert(2, "您的账户还未进行资质认证，请先进行资质认证!");
							location.href = '../approve/approve_main.html';
						}
					});
					
					$(".productDetail").unbind("click").click(function() {
						var productId = $(this).attr("id");
						location.href = '../product/productDetail.html?productId=' + productId;

					});

				} else {
					base.alert(2, "信息获取失败");
				}
			}
		});
	}

	return {
		init: init
	};
});